package com.struts.action;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;

@SuppressWarnings("serial")
public class ValueStackAction extends ActionSupport{
	
	/**
	 * 获取值栈对象的三种方式
	 * @return
	 */
	public String testValueStack01() {
		//通过ActionContext实例获取ValueStack对象
		ValueStack valueStack = ActionContext.getContext().getValueStack() ;
		ValueStack valueStack2 = ServletActionContext.getContext().getValueStack() ;
		//通过request域对象获取ValueStack对象
		ValueStack valueStack3 = (ValueStack) ServletActionContext.getRequest().getAttribute("struts.valueStack") ;
		System.out.println(valueStack);
		System.out.println(valueStack2);
		System.out.println(valueStack3);
		return SUCCESS ;
	}
	
	/**
	 * 获取值栈对象中的内存数据结构
	 */
	public String testValueStack02() {
		//获取值栈对象
		ValueStack valueStack = ActionContext.getContext().getValueStack() ;
		//获取对象栈实例化对象root
		CompoundRoot root = valueStack.getRoot() ;
		//遍历输出root对象中的内容
		for (int i = 0; i < root.size(); i++) {
			System.out.println(root.get(i));
		}
		//获取OgnlContext对象context
		Map<String, Object> context = valueStack.getContext() ;
		//遍历输入map中的元素
		for (Entry<String, Object> entry : context.entrySet()) {
			System.out.println(entry.getValue().getClass());
		}
		return SUCCESS ;
	} 
	
}
